<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2019/3/9
 * Time: 16:55
 */

namespace app\admin\controller;

use app\admin\model\Rechange as MoneyLogModel;
use app\admin\validate\Rechange as MoneyLogValidate;
use think\db\Where;

class Vip extends Base {

    public function __construct(){
        parent::__construct();
        $this->model = new MoneyLogModel;
        $this->validate = new MoneyLogValidate;
    }

    /**
     * @param int $limit
     * @param int $page
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index($limit=10,$page=1){
        $where=new Where();
        $data = request()->post();
        $where['type']=1;
        $where['status']=['neq',1];
        if($data){
            if($data['title']){
                $where['title']=['like','%'.trim($data['title']).'%'];
            }
            if($data['status']){
                $where['status']=$data['status'];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where['create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $this->model->getList($where,$limit);
//        p($list);die;
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    public function add($id=0){
        if($id){
            $info = $this->model->find($id);
            $this->assign('info',$info);
        }
        return view();
    }

    /**
     * 删除
     * @param int $id
     * @throws \Exception
     */
    public function del($id=0){
        if(!$id){
            $this->__e('缺少参数');
        }
        if(!$this->model->where('id','eq',$id)->delete()){
            $this->__e('删除失败');
        }
        $this->__s('删除成功');
    }

    public function vip($id=0){
        if(!$id){
            return $this->__e('缺少参数');
        }
        $info = $this->model->get($id);
        if(!$info){
            return $this->__e('没有数据');
        }
        if($info['status']!=2){
            return $this->__e('订单未成功');
        }
        $mch = new \app\admin\model\Mch();
        $_info = $mch->get($info['mc_id']);
        if(!$_info){
            return $this->__e('商户不存在');
        }
        if($_info['is_vip']==1){
            return $this->__e('已经是VIP了');
        }
        $_info->is_vip = 1;
        if(!$_info->save()){
            return $this->__e('操作失败');
        }
        return $this->__s('操作成功');
    }
}